package org.smile.config;

import org.smile.beans.converter.BeanException;
import org.smile.beans.handler.MapPropertyHandler;
import org.smile.io.IOUtils;
import org.smile.template.SimpleStringTemplate;
import org.smile.template.Template;
import org.smile.util.Properties;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class YmlProperties extends Properties{

	@Override
	public void load(InputStream is, Object initParam, boolean closeIs) throws IOException {
		try {
			String xml = IOUtils.readString(is);
			if (initParam != null) {
				Template template = new SimpleStringTemplate(xml);
				xml = template.processToString(initParam);
			}
			Yaml yaml = new Yaml();
			Map map = yaml.loadAs(xml, Map.class);
			this.putAll(map);
		}finally {
			if(closeIs){
				IOUtils.close(is);
			}
		}
	}
}

